﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Extensions.cs" company="">
//   
// </copyright>
// <summary>
//   The extensions.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace F5.BigIp.LocalTrafficManager.Proxy.V11
{
    using System.Linq;

    using F5.BigIp.LocalTrafficManager.Interfaces;
    using F5.BigIp.LocalTrafficManager.Proxy.V11.LocalLB.Pool;

    /// <summary>The extensions.</summary>
    public static class Extensions
    {
        #region Public Methods and Operators

        /// <summary>The current connections.</summary>
        /// <param name="poolMembersStatistics">The pool members statistics.</param>
        /// <returns>The <see cref="int"/>.</returns>
        public static PoolMemberStatistics ToPoolMemberStatistics(
            this LocalLBPoolMemberStatisticEntry poolMembersStatistics)
        {
            return new PoolMemberStatistics
                       {
                           CurrentConnections =
                               (int)
                               poolMembersStatistics.statistics.First(
                                   k =>
                                   k.type
                                   == CommonStatisticType.STATISTIC_SERVER_SIDE_CURRENT_CONNECTIONS)
                                                    .value.low
                       };
        }

        /// <summary>The get pool member status.</summary>
        /// <param name="poolMemberStatus">The pool members status.</param>
        /// <returns>The <see cref="PoolMemberStatus"/>.</returns>
        public static PoolMemberStatus ToPoolMemberStatus(this LocalLBObjectStatus poolMemberStatus)
        {
            //// https://devcentral.f5.com/wiki/iControl.LocalLB__AvailabilityStatus.ashx
            switch (poolMemberStatus.enabled_status)
            {
                case LocalLBEnabledStatus.ENABLED_STATUS_ENABLED:

                    if (poolMemberStatus.availability_status == LocalLBAvailabilityStatus.AVAILABILITY_STATUS_GREEN)
                    {
                        return PoolMemberStatus.Enabled;
                    }

                    break;

                case LocalLBEnabledStatus.ENABLED_STATUS_DISABLED:

                    if (poolMemberStatus.availability_status == LocalLBAvailabilityStatus.AVAILABILITY_STATUS_GREEN)
                    {
                        return PoolMemberStatus.Disabled;
                    }

                    if (poolMemberStatus.availability_status == LocalLBAvailabilityStatus.AVAILABILITY_STATUS_RED)
                    {
                        return PoolMemberStatus.Offline;
                    }

                    break;
            }

            return PoolMemberStatus.Unknown;
        }

        #endregion
    }
}